package cn.tedu._08security.base.config;

import cn.tedu._08security.service.UserDetailServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)     //激活鉴权
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /*
    工程启动时，创建了一个UserDetailsService放入spring容器中
     */
    @Bean
    public UserDetailsService userDetailService() {
        return new UserDetailServiceImpl();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
                //配置请求授权规则
                authorizeRequests()
                .antMatchers("/api/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
                //配置表单登录
                .formLogin()
                .defaultSuccessUrl("/index.html")
                .permitAll()
                .and()
                //配置退出登录
                .logout()
                .permitAll();
    }
}
